//
//  GNStatusCell.swift
//  SinaWeChat
//
//  Created by mac on 15/12/10.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

/**
 整个 cell 分成三大块:原创微博的内容,转发微博的内容,底部显示转发评论赞的 toolBar
 */
 // cell 内部子控件共同的间距

let GNStatusCellMargin: CGFloat = 10

class GNStatusCell: UITableViewCell{
    
    
    //toolBar 顶部的约束
    var toolBarTopConstraint:Constraint?
    
    var  statusViewModel:GNStatusViewModel? {
        
        didSet{
            
            //当 '外界' 设置这个属性的时候,会调用 didSet
             // 也就是说我们可以在这个方法里面填充控件内容
            originalView.statusViewModel = statusViewModel
            statusToolbar.statusViewModel = statusViewModel
            
            //弄掉旧的装新的
            toolBarTopConstraint?.uninstall()
            
            //SnpKit
            if statusViewModel?.status?.retweeted_status != nil {
                // 转发微博存在.
                // 需要将其显示.因为cell是可以复用的
                retweetView.hidden = false
                retweetView.statusViewModel = statusViewModel
                //更新约束
                
                statusToolbar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else{
                
                //转发微博不存在
                retweetView.hidden = true
                //更新约束
                statusToolbar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            
        }
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        
        // 1. 添加 View
        contentView.addSubview(originalView)
        contentView.addSubview(statusToolbar)
        contentView.addSubview(retweetView)
        
        //2.添加约束
        
        //原创微博的View
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        //转发微博
        
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(originalView)
            
        }
        
        //底部 toolBar
        
        statusToolbar.snp_makeConstraints { (make) -> Void in
            
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.leading.equalTo(contentView)
            make.width.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //整体的cell高度约束very important
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(statusToolbar)
            
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
        
    }
    
    
    //懒加载
    
    private lazy var originalView: GNStatusOriginalView = GNStatusOriginalView()
    
    private lazy var statusToolbar :GNStatusToolBar = GNStatusToolBar()
    
    private lazy var retweetView : GNStatusRetweetView = GNStatusRetweetView()
}
